#ifndef mylistener_h_
#define mylistener_h_
#include <uifcDefaultListeners.h>
#include <pfcGlobal.h>
#include "logger.h"
#include <pfcExceptions.h>

void ChangeCellText(uifcTable_ptr handle);
void DeleteCellText(uifcTable_ptr handle);
class MyTableListener : public uifcDefaultTableListener
{
    void OnBeginDrag(uifcTable_ptr handle) {}
    void OnKeyDown(uifcTable_ptr handle)
    {
        uifcKey_ptr keypress = handle->GetKeyPressed();
        try
        {
            switch (keypress->GetId())
            {
            case uifcKEY_DELETE:
                DeleteCellText(handle);
                break;

            default:
                break;
            }
        }
        OTK_EXCEPTION_PRINT_LOG
    }
    void OnDropNowhere(uifcTable_ptr handle) {}
    void OnBeginCellSelect(uifcTable_ptr handle) {}
    void OnColumnSelect(uifcTable_ptr handle)
    {
        handle->SetSelectedCellNameArray(xstringsequence::create());
        handle->SetSelectedRowNameArray(xstringsequence::create());
        xstringsequence_ptr sel = handle->GetSelectedColumnNameArray();
        handle->SelectCellsOfColumnByName(sel, true);
    }
    void OnDragNowhere(uifcTable_ptr handle) {}
    void OnDestroy(uifcTable_ptr handle) {}
    void OnCellActivate(uifcTable_ptr handle)
    {
        ChangeCellText(handle);
    }
    void OnCellHover(uifcTable_ptr handle) {}
    void OnRowSelect(uifcTable_ptr handle)
    {
        handle->SetSelectedCellNameArray(xstringsequence::create());
        handle->SetSelectedRowNameArray(xstringsequence::create());
        xstringsequence_ptr sel = handle->GetSelectedRowNameArray();
        handle->SelectCellsOfRowByName(sel, true);
    }
    void OnEndCellSelect(uifcTable_ptr handle)
    {
    }
    void OnEndDrag(uifcTable_ptr handle) {}
    void OnDrop(uifcTable_ptr handle) {}
    void OnFocusOut(uifcTable_ptr handle) {}
    void OnDragExit(uifcTable_ptr handle) {}
    void OnFocusIn(uifcTable_ptr handle) {}
    void OnDragMove(uifcTable_ptr handle) {}
    void OnHelp(uifcTable_ptr handle) {}
    void OnCellSelect(uifcTable_ptr handle) {}
    void OnDragEnter(uifcTable_ptr handle) {}
};
enum MyButtonAction
{
    BTN_CLOSE,
    BTN_GET_LIST,
    BTN_START,
    BTN_nil
};
class ChangeTableInfo : public uifcDefaultPushButtonListener
{
private:
    uifcTable_ptr uitable;
    pfcTable_ptr pfctable;

public:
    ChangeTableInfo(uifcTable_ptr handle, pfcTable_ptr table)
    {
        uitable = handle;
        pfctable = table;
    }

public:
    void OnActivate(uifcPushButton_ptr handle)
    {
        xstringsequence_ptr row_name = uitable->GetRowNameArray();
        xstringsequence_ptr col_name = uitable->GetColumnNameArray();
        xint rcount = row_name->getarraysize();
        xint ccount = col_name->getarraysize();
        for (xint r = 0; r < rcount; r++)
        {
            for (xint c = 0; c < ccount; c++)
            {
                xstring text;
                uifcTableCell_ptr uicell = uifcTableCellLookup(handle->GetDialog(), uitable->GetComponent(), r, c);
                text = uicell->GetText();
                xstringsequence_ptr texts = xstringsequence::create();
                texts->append(text);
                pfcTableCell_ptr TableCell = pfcTableCell::Create(r + 1, c + 1);
                pfctable->SetText(TableCell, texts);
            }
        }
        uifcExitDialog(handle->GetDialog(), 0);
    }
};
void ChangeCellText(uifcTable_ptr handle)
{
    pfcSession_ptr session = pfcGetProESession();
    try
    {
        xstring text = session->UIReadStringMessage(false);
        xstring CellSelected = handle->GetSelectedCellNameArray()->get(0);
        uifcTableCellCoordinates_ptr coor = handle->GetAnchorCellName();
        uifcTableCell_ptr cell = uifcTableCellFind(handle->GetDialog(), handle->GetComponent(),
                                                   coor->GetRow(), coor->GetColumn());
        if (!(text.IsNull() || text == xstring("use_default_string")))
            cell->SetText(text);
        session->UIClearMessage();
    }
    OTK_EXCEPTION_PRINT_LOG
}
void DeleteCellText(uifcTable_ptr handle)
{
    xstringsequence_ptr cells = handle->GetSelectedCellNameArray();
    try
    {
        xint num = cells->getarraysize();
        if (num > 0)
        {
            for (int i = 0; i < num / 2; i++)
            {
                uifcTableCell_ptr cell = uifcTableCellFind(handle->GetDialog(), handle->GetComponent(),
                                                           cells->get(2 * i), cells->get(2 * i + 1));
                cell->SetText(xstring(""));
            }
        }
    }
    OTK_EXCEPTION_PRINT_LOG
}

#endif